package com.apes.fico.engine.model;

import com.apes.fico.masterdata.gl.model.AccountingOperatingChart;
import com.apes.fico.masterdata.gl.model.CashFlow;
import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: apestech
 * @description: 凭证分录
 * @author: Chengxm
 * @create: 2018-12-24 14:50
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TempVoucherItem.CONST_TABLE_NAME)
public class TempVoucherItem implements ApesBean {
    public final static String CONST_TABLE_NAME = "gl_voucher_item_temp";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "财务凭证分录", padding = 8)
    @Comment("凭证分录")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "voucher_id", referencedColumnName = "id")
    @Comment("凭证内码")
    private TempVoucher voucher;

    @NotNull
    @ManyToOne()
    @JoinColumn(name = "acc_operating_chart_id", referencedColumnName = "id")
    @Comment("科目")
    private AccountingOperatingChart accountingOperatingChart;

    @Column(name = "debit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("借方金额")
    private double debit = 0;

    @Column(name = "credit", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("贷方金额")
    private double credit = 0;

    @Comment("摘要")
    @Column(name = "summary", length = 400)
    private String summary;

    @OneToMany(mappedBy = "voucherItem", cascade = CascadeType.ALL) //, orphanRemoval = true
    private List<TempVoucherAuxiliary> voucherAuxiliaryList = new ArrayList<>();

    public List<TempVoucherAuxiliary> getVoucherAuxiliaryList() {
        if (voucherAuxiliaryList == null) return null;
        for (TempVoucherAuxiliary voucherAuxiliary : voucherAuxiliaryList) {
            voucherAuxiliary.setVoucherItem(this);
        }
        return voucherAuxiliaryList;
    }

    @Column(name = "collect_aux", length = 32) //, nullable = false
    @Comment("辅助核算归集key")
    private String collectAux;

    public String getCollectAux() {
        if (this.getVoucherAuxiliaryList()== null) return null;
        List combination = new ArrayList();
        this.getVoucherAuxiliaryList().forEach(voucherAuxiliarys -> {
            String code = voucherAuxiliarys.getAccountingOperatingChartAuxiliary().getAccAuxiliaryProject().getName();
            String name = voucherAuxiliarys.getName();
            if (code != null && name != null) {
                combination.add(code + ":" + "[" + name + "]");
            }
        });
        return combination.toString().substring(1, combination.toString().length() - 1);
    }

    @ManyToOne()
    @JoinColumn(name = "cash_flow_id", referencedColumnName = "id")
    @Comment("现金流量项目")
    private CashFlow cashFlow;

    @ManyToOne()
    @JoinColumn(name = "voucher_template_entry_id", referencedColumnName = "id")
    @Comment("模板分录")
    private VoucherTemplateEntry voucherTemplateEntry;

    @Name
    @Transient
    private String key;

    public String getKey() {
        if (!StringUtils.isEmpty(key)) return key;
        if (this.voucherAuxiliaryList == null) return null;
        List combination = new ArrayList();
        this.getVoucherAuxiliaryList().forEach(voucherAuxiliarys -> {
            String id = voucherAuxiliarys.getId();
            String name = voucherAuxiliarys.getAccountingOperatingChartAuxiliary().getAccAuxiliaryProject().getName();
            combination.add("[" + id + "]" + "[" + name + "]");
        });
        return combination.toString();
    }

    public Merge merge() {
        return new Merge(voucherTemplateEntry, accountingOperatingChart, collectAux, summary, voucherTemplateEntry.getOrient(), cashFlow);
    }

    @Data
    public static class Merge {
        /**
         * 模板分录
         **/
        private VoucherTemplateEntry voucherTemplateEntry;
        /**
         * 科目
         **/
        private AccountingOperatingChart accountingOperatingChart;
        /**
         * 辅助核算归集key
         **/
        private String collectAux;
        /**
         * 摘要
         **/
        private String summary;

        /**
         * 借贷方向
         **/
        private String orient;

        /**现金流量**/
        private CashFlow cashFlow;

        Merge(VoucherTemplateEntry voucherTemplateEntry, AccountingOperatingChart accountingOperatingChart,
              String collectAux, String summary, String orient, CashFlow cashFlow) {
            this.voucherTemplateEntry = voucherTemplateEntry;
            this.accountingOperatingChart = accountingOperatingChart;
            this.collectAux = collectAux;
            this.summary = summary;
            this.orient = orient;
            this.cashFlow = cashFlow;
        }
    }

}
